﻿/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.javascript {
    import flash.events.*;
    import flash.external.ExternalInterface;
    import flash.utils.Timer;

	[Event(name="received",type="com.puny.javascript.JavaScriptEvent")]
	
	/**
	 * @private
	 */
    public class JavaScript extends EventDispatcher {
//        private var input:TextField;
//        private var output:TextField;
//        private var sendBtn:Sprite;

        public function JavaScript(checkContainer:Boolean=false) {
            if (ExternalInterface.available) {
                try {
                    trace("Adding callback...");
                    ExternalInterface.addCallback("sendToActionScript", receivedFromJavaScript);
                    if (checkContainer) {
	                    if (checkJavaScriptReady()) {
	                        trace("JavaScript is ready.");
	                    } else {
	                        trace("JavaScript is not ready, creating timer.");
	                        var readyTimer:Timer = new Timer(100, 0);
	                        readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);
	                        readyTimer.start();
	                    }
	                } else {
	                	trace("Assume JavaScript is ready.");
	                }
                } catch (error:SecurityError) {
                    trace("A SecurityError occurred: " + error.message );
                } catch (error:Error) {
                    trace("An Error occurred: " + error.message);
                }
            } else {
                trace("External interface is not available for this container.");
            }
        }
        private function receivedFromJavaScript(value:String):void {
            trace("JavaScript says: " + value );
			var event:JavaScriptEvent = new JavaScriptEvent(JavaScriptEvent.RECEIVED);
			event.receivedValue = value;
			dispatchEvent(event);
        }
        private function checkJavaScriptReady():Boolean {
            var isReady:Boolean = ExternalInterface.call("isReady");
            return isReady;
        }
        private function timerHandler(event:TimerEvent):void {
            trace("Checking JavaScript status...");
            var isReady:Boolean = checkJavaScriptReady();
            if (isReady) {
                trace("JavaScript is ready.");
                Timer(event.target).stop();
            }
        }
		public function invoke(command:String,...parameters:Array):* {
			var received:*;
            if (ExternalInterface.available) {
                received = ExternalInterface.call(command, parameters);
            } else {
				trace("External interface is not available");
			}
			return received;
		}
		public function readCookie(name:String):String {
			return invoke("readCookie", name);
		}
    }
}
